<template>
  <div class="app-container">
    <!-- 查询条件开始 -->
    <el-form ref="queryForm" :model="queryParams" :inline="true" label-width="88px">
      <el-form-item label="客户名称" prop="customer">
        <el-select v-model="queryParams.customer" filterable placeholder="请选择客户">
          <el-option
            v-for="item in allCustomerOptions"
            :key="item.id"
            :label="item.customer"
            :value="item.customer"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="站点名称" prop="site">
        <el-input
          v-model="queryParams.site"
          placeholder="请输入站点名称"
          clearable
          size="small"
          style="width:180px"
        />
      </el-form-item>
      <el-form-item label="省份" prop="address">
        <el-select
          v-model="queryParams.address"
          placeholder="请选择省份"
          clearable
          size="small"
          style="width:180px"
        >
          <el-option
            v-for="address in addressOptions"
            :key="address.id"
            :label="address.deployment"
            :value="address.deployment"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="客户来源" prop="sourceofcustomer">
        <el-select
          v-model="queryParams.sourceofcustomer"
          placeholder="请选择客户来源"
          clearable
          size="small"
          style="width:180px"
        >
          <el-option
            v-for="deploy in customerTypeOptions"
            :key="deploy.id"
            :label="deploy.deployment"
            :value="deploy.deployment"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="创建时间">
        <el-date-picker
          v-model="dateRange"
          size="small"
          style="width:230px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholde="开始日期"
          end-placeholde="结束日期"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button type="warning" icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <!-- 查询条件结束 -->

    <!-- 表格工具按钮开始 -->
    <el-row :gutter="10" style="margin-bottom: 8px;">
      <el-col :span="1.5">
        <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAdd">添加</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete">删除</el-button>
      </el-col>
    </el-row>
    <!-- 表格工具按钮结束 -->

    <!-- 数据表格开始 -->
    <el-table v-loading="loading" element-loading-text="正在加载中" border :data="customerTableList" max-height="660" @selection-change="handleSelectionChnage">
      <el-table-column type="selection" width="55" align="center" fixed="left" />
      <el-table-column label="ID" align="center" prop="id" />
      <el-table-column label="CustID" align="center" prop="custid" />
      <el-table-column label="客户名称" align="center" prop="customer" width="180" />
      <el-table-column label="站点名称" align="center" prop="site" width="180" />
      <el-table-column label="姓名" align="center" prop="person" />
      <el-table-column label="职务" align="center" prop="post" />
      <el-table-column label="联系方式" align="center" prop="telephone" width="120" />
      <el-table-column label="帮我吧ID" align="center" prop="help" width="120" />
      <el-table-column label="省份" align="center" prop="address" width="120" />
      <el-table-column label="城市" align="center" prop="city" width="120" />
      <el-table-column label="站点数量" align="center" prop="sitenum" />
      <el-table-column label="部署方式" align="center" prop="deployment" />
      <el-table-column label="95A1" align="center" prop="c95a1" width="150" />
      <el-table-column label="中心数据库" align="center" prop="centerdb" />
      <el-table-column label="9581零管" align="center" prop="c9581" />
      <el-table-column label="传输" align="center" prop="transfer" />
      <el-table-column label="9001制卡" align="center" prop="c9001" width="150" />
      <el-table-column label="95A2网点" align="center" prop="c95a2" width="150" />
      <el-table-column label="9511油站" align="center" prop="c9511" width="150" />
      <el-table-column label="9511数据库" align="center" prop="c9511db" />
      <el-table-column label="7100便利店" align="center" prop="c7100" width="150" />
      <el-table-column label="7800便利店中心" align="center" prop="c7800" width="150" />
      <el-table-column label="9512" align="center" prop="c9512" />
      <el-table-column label="9599室内扣款" align="center" prop="c9599" />
      <el-table-column label="云智付" align="center" prop="cloud" />
      <el-table-column label="青蛙付" align="center" prop="frog" />
      <el-table-column label="32寸屏" align="center" prop="p32" />
      <el-table-column label="15寸屏" align="center" prop="p15" />
      <el-table-column label="5.8寸屏" align="center" prop="p58" />
      <el-table-column label="圈存机" align="center" prop="transference" />
      <el-table-column label="自助服务机" align="center" prop="selfservice" />
      <el-table-column label="掌厅" align="center" prop="palmhall" />
      <el-table-column label="掌站" align="center" prop="masterstation" />
      <el-table-column label="核销小程序" align="center" prop="closure" />
      <el-table-column label="移动支付接口" align="center" prop="payment" />
      <el-table-column label="支付通道" align="center" prop="paychannel" />
      <el-table-column label="线上/线下支付" align="center" prop="onlineandoffline" />
      <el-table-column label="账户数量" align="center" prop="accounts" />
      <el-table-column label="云闪付" align="center" prop="flashpay" />
      <el-table-column label="产品类型" align="center" prop="product" width="150" />
      <el-table-column label="备注" align="center" prop="message" />
      <el-table-column label="油机使用情况" align="center" prop="oilengine" />
      <el-table-column label="清分清算" align="center" prop="clean" />
      <el-table-column label="特殊应用" align="center" prop="specialapp" />
      <el-table-column label="其他应用" align="center" prop="otherapp" />
      <el-table-column label="客户来源" align="center" prop="sourceofcustomer" />
      <el-table-column label="客户类型" align="center" prop="customertype" />
      <el-table-column label="状态" align="center" prop="status" />
      <el-table-column label="创建人" align="center" prop="founder" />
      <el-table-column label="创建时间" align="center" prop="createtime" width="160" />
      <el-table-column label="更新人" align="center" prop="updatebody" />
      <el-table-column label="更新时间" align="center" prop="updatetime" width="160" />
      <el-table-column label="问题处理状态" align="center" prop="problem" />
      <el-table-column label="操作" align="center" width="180" fixed="right">
        <template slot-scope="scope">
          <el-button type="text" icon="el-icon-edit" size="mini" @click="handleUpdate(scope.row)">修改</el-button>
          <el-button type="text" icon="el-icon-delete" size="mini" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 数据表格结束 -->
    <!-- 分页控件开始 -->
    <el-pagination
      v-show="total>0"
      :current-page="queryParams.pageNum"
      :page-sizes="[20, 30, 50, 100]"
      :page-size="queryParams.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
    <!-- 分页控件结束 -->

    <!-- 添加修改弹出层开始 -->
    <el-dialog
      :title="title"
      :visible.sync="open"
      width="1600px"
      center
      append-to-body
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-row>
          <el-col :span="4">
            <el-form-item label="客户名称" prop="customer">
              <el-input v-model="form.customer" placeholder="请输入客户名称" clearable size="small" />
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="站点名称" prop="site">
              <el-input v-model="form.site" placeholder="请输入站点名称" clearable size="small" />
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="联系方式" prop="telephone">
              <el-input v-model="form.telephone" placeholder="请输入联系方式" clearable size="small" />
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="产品类型" prop="product">
              <el-input v-model="form.product" placeholder="请输入产品类型" clearable size="small" />
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="省份地区" prop="address">
              <el-cascader
                v-model="addressData"
                :options="addressSelectList"
                size="small"
                :show-all-levels="true"
                :props="selectAddressProps"
                collapse-tags
                clearable
                @change="handleChange"
              />
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="客户类型" prop="customertype">
              <el-input v-model="form.customertype" placeholder="请输入客户类型" clearable size="small" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-row>
          <el-col :span="4">
            <el-form-item label="站点数量" prop="sitenum">
              <el-input-number v-model="form.sitenum" clearable size="small" label="站点数量" />
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="CustId" prop="custid">
              <el-input v-model="form.custid" placeholder="请输入CustId" clearable size="small" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="handleSubmit">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </span>
    </el-dialog>
    <!-- 添加修改弹出层结束 -->
  </div>
</template>
<script>
// 引入api
// eslint-disable-next-line no-unused-vars
import { listCustomerForPage, getCustomerById, queryAddressNode, addCustomer, deleteCustomerByIds, updateCustomer, getAddressData } from '@/api/system/customer'

export default {
  // 定义页面数据
  data() {
    return {
      props: { multiple: true },
      selectAddressProps: { multiple: false, expandTrigger: 'hover' },
      // 是否启用遮罩层
      loading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 分页数据总条数
      total: 0,
      // 字典表格数据
      customerTableList: [],
      // 省份市区层次json数据
      addressSelectList: [],
      // 回显数据
      addressData: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 状态数据字典
      customerTypeOptions: [],
      // 公告类型
      addressOptions: [],
      // 客户数据
      allCustomerOptions: [],
      // 日期范围
      dateRange: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 20,
        customer: undefined,
        site: undefined,
        address: undefined,
        sourceofcustomer: undefined
      },
      // 表单数据
      form: {},
      // 表单校验
      rules: {
        customer: [
          { required: true, message: '客户名称不能为空', trigger: 'blur' }
        ],
        customertype: [
          { required: true, message: '客户类型不能为空', trigger: 'blur' }
        ],
        product: [
          { required: true, message: '产品类型不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  // 勾子
  created() {
    // 使用全局的根据字典类型查询字典数据的方法查询字典数据
    this.loadDataSelect('2').then(res => {
      this.customerTypeOptions = res.data
    })
    this.loadDataSelect('3').then(res => {
      this.addressOptions = res.data
    })
    this.queryAllCustomer('正常').then(res => {
      this.allCustomerOptions = res.data
    })
    // 查询表格数据
    this.getCustomerList()
    this.getAddressSelectList()
  },
  // 方法
  methods: {
    handleChange(value) {
      this.form.address = value[0]
      this.form.city = value[1]
    },
    // 查询表格数据
    getCustomerList() {
      this.loading = true // 打开遮罩
      listCustomerForPage(this.addDateRange(this.queryParams, this.dateRange)).then(res => {
        this.customerTableList = res.data
        this.total = res.total
        this.loading = false// 关闭遮罩
      })
    },
    // 加载省份城市层次json数据
    getAddressSelectList() {
      queryAddressNode().then(res => {
        this.addressSelectList = res.data
      })
    },
    // 条件查询
    handleQuery() {
      this.getCustomerList()
    },
    // 重置查询条件
    resetQuery() {
      this.resetForm('queryForm')
      this.dateRange = []
      this.getCustomerList()
    },
    // 数据表格的多选择框选择时触发
    handleSelectionChnage(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    // 分页pageSize变化时触发
    handleSizeChange(val) {
      this.queryParams.pageSize = val
      // 重新查询
      this.getCustomerList()
    },
    // 点击上一页  下一页，跳转到哪一页面时触发
    handleCurrentChange(val) {
      this.queryParams.pageNum = val
      // 重新查询
      this.getCustomerList()
    },
    // 打开添加的弹出层
    handleAdd() {
      this.reset()
      this.addressData = []
      this.open = true
      this.title = '添加客户档案'
    },
    // 打开修改的弹出层
    handleUpdate(row) {
      this.title = '修改客户档案'
      const customerId = row.id || this.ids
      this.open = true
      this.reset()
      this.loading = true
      getCustomerById(customerId).then(res => {
        this.form = res.data
        getAddressData(customerId).then(res => {
          this.addressData = res.data
        })
        this.loading = false
      })
    },
    // 执行删除
    handleDelete(row) {
      const customerIds = row.id || this.ids
      this.$confirm('此操作将永久删除该客户档案, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.loading = true
        deleteCustomerByIds(customerIds).then(res => {
          this.loading = false
          this.msgSuccess('删除成功')
          this.getCustomerList()// 全查询
        }).catch(() => {
          this.msgError('删除失败')
          this.loading = false
        })
      }).catch(() => {
        this.msgError('删除已取消')
        this.loading = false
      })
    },
    // 确定
    handleSubmit() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          // 做添加
          this.loading = true
          if (this.form.id === undefined) {
            addCustomer(this.form).then(res => {
              this.msgSuccess('保存成功')
              this.reset()
              this.loading = false
              this.getCustomerList()// 列表重新查询
              this.open = false// 关闭弹出层
            }).catch(() => {
              this.loading = false
            })
          } else { // 做修改
            updateCustomer(this.form).then(res => {
              this.msgSuccess('修改成功')
              this.loading = false
              this.getCustomerList()// 列表重新查询
              this.open = false// 关闭弹出层
            }).catch(() => {
              this.loading = false
            })
          }
        }
      })
    },
    // 取消
    cancel() {
      this.open = false
      this.title = ''
      this.reset()
    },
    // 重置表单
    reset() {
      this.resetForm('form')
      this.form = {
        customer: undefined,
        noticeContent: undefined,
        address: undefined,
        sourceofcustomer: undefined,
        sitenum: 1,
        addressData: [],
        addressSelectList: []
      }
    }
  }
}
</script>
